/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 combat_object_fader.cpp

	$Header: /heroes4/combat_object_fader.cpp $

	$NoKeywords: $

 ************************************************************************/
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "combat_object_fader.h"

#include "battlefield.h"
#include "battlefield_ptr.h"
// ----------------------------------------------------------------------
// class to fade creature image in or out
// ----------------------------------------------------------------------
t_combat_object_fader::t_combat_object_fader( t_battlefield& battlefield, 
											  t_abstract_combat_object* object,
								              bool fade_in )
				: t_counted_animation( battlefield ), m_object( object ), t_counted_idle_processor( 75 )
{
	m_fading_in = fade_in;
}

// ----------------------------------------------------------------------
// class to fade creature image in or out
// ----------------------------------------------------------------------
void t_combat_object_fader::on_idle()
{
	t_combat_object_fader_ptr ref = this;

	if (m_fading_in)
	{
		if (m_object->get_alpha() < m_object->get_maximum_alpha())
		{
			m_object->set_alpha( m_battlefield, m_object->get_alpha() + 1 );
			return;
		}
		m_object->set_alpha( m_battlefield, m_object->get_maximum_alpha() );
	}
	else
	{
		if (m_object->get_alpha() > 0)
		{
			m_object->set_alpha( m_battlefield, m_object->get_alpha() - 1 );
			return;
		}
	}
	
	if (m_animation_ended)
		return;

	// build a reference to battlefield to avoid case where m_handler() destroys it.
	// we don't want this as a data member because it could cause circular references.

	t_battlefield_ptr battlefield_ref = &m_battlefield;

	suspend_idle_processing();
	m_object->clear_fader();
	animation_ended();
	m_handler();
	m_battlefield.update_state();
	m_object = 0;
}
